package apk.yoursms;

import java.util.Locale;

import org.acra.ACRA;
import org.acra.ErrorReporter;
import org.acra.ReportingInteractionMode;
import org.acra.annotation.ReportsCrashes;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;

import android.app.Application;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.preference.PreferenceManager;

@ReportsCrashes(formKey = "dHhVVVJHcFcxbHJiVUlQVjJZSXJPVGc6MQ", logcatArguments = {
	"-t", "100", "-v", "long", "ActivityManager:I", "YourSms:D", "*:S" }, mode = ReportingInteractionMode.TOAST, resToastText = R.string.crash_toast_text)
public class YourSms extends Application {

    private SharedPreferences preferences;
    private Locale locale;
    private String lang;
    private HttpClient httpClient;

    @Override
    public void onCreate() {
	ACRA.init(this);
	ErrorReporter.getInstance().checkReportsOnApplicationStart();
	super.onCreate();

	preferences = PreferenceManager.getDefaultSharedPreferences(this);
	lang = preferences.getString("lang", "default");
	if (lang.equals("default")) {
	    lang = getResources().getConfiguration().locale.getCountry();
	}
	locale = new Locale(lang);
	Locale.setDefault(locale);
	Configuration config = new Configuration();
	config.locale = locale;
	getBaseContext().getResources().updateConfiguration(config, null);

	httpClient = createHttpClient();
    }

    @Override
    public void onLowMemory() {
	super.onLowMemory();
	shutDownHttpClient();
    }

    @Override
    public void onTerminate() {
	super.onTerminate();
	shutDownHttpClient();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
	super.onConfigurationChanged(newConfig);
	locale = new Locale(lang);
	Locale.setDefault(locale);
	Configuration config = new Configuration();
	config.locale = locale;
	getBaseContext().getResources().updateConfiguration(config, null);
    }

    private HttpClient createHttpClient() {
	HttpParams params = new BasicHttpParams();
	HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
	HttpProtocolParams.setContentCharset(params,
		HTTP.DEFAULT_CONTENT_CHARSET);
	HttpProtocolParams.setUseExpectContinue(params, true);

	SchemeRegistry schReg = new SchemeRegistry();
	schReg.register(new Scheme("http", PlainSocketFactory
		.getSocketFactory(), 80));
	ClientConnectionManager conMgr = new ThreadSafeClientConnManager(
		params, schReg);

	return new DefaultHttpClient(conMgr, params);
    }

    private void shutDownHttpClient() {
	if (httpClient != null && httpClient.getConnectionManager() != null) {
	    httpClient.getConnectionManager().shutdown();
	}
    }

    public HttpClient getHttpClient() {
	return httpClient;
    }

}
